package com.aco.party.web.controller.system;

import com.aco.party.model.po.${poNameC};
import com.aco.party.model.vo.${poNameC}Vo;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.${poNameC}Service;
import com.aco.party.service.system.MetaService;
import com.aco.party.web.command.system.Edit${poNameC}Command;
import com.aco.party.web.validator.system.Edit${poNameC}Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import static com.aco.party.util.WebModelUtil.*;
/**
 * Created by flying on 3/10/2016 0010.
 */
@Controller
@RequestMapping("/system")
public class Manage${poNameC}Controller extends BaseController {
    Logger logger = LoggerFactory.getLogger(Manage${poNameC}Controller.class);

    @Resource
    private ${poNameC}Service ${poName}Service;

    @Resource
    private MetaService metaService;

    private Edit${poNameC}Validator validator = new Edit${poNameC}Validator();

    private final String REDIRECTEDIT = "redirect:edit";

    @RequestMapping(value = {"/${poName}", "/${poName}/list"}, method = RequestMethod.GET)
    public String showManagePage(HttpServletResponse response) {
        return "system/manage${poNameC}s";
    }

    @RequestMapping(value = "${poName}/list", method = RequestMethod.POST)
    @ResponseBody
    public Page
            <${poNameC}Vo> getListJson(HttpServletRequest request, DataGridEntry entry) {
        Page <${poNameC}Vo> page = new Page<>();
        entry.webParamConvert();
        List <${poNameC}Vo> list = ${poName}Service.getByPage(new PageParam(entry.getOffset(), entry.getLimit()));
        page.setTotal(${poName}Service.getTotal());
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/${poName}/edit", method = RequestMethod.GET)
    public String showEdit(HttpServletResponse response,@ModelAttribute Edit${poNameC}Command command, Model model) {
        if (command.getId() != null) {
            ${poNameC} po = ${poName}Service.getByPrimaryKey(command.getId());
            if (po != null)
                command.toCommand(po);
            else
                model.addAttribute("no_obj", true);
        }
        return "system/edit${poNameC}";
    }

    @RequestMapping(value = "/${poName}/edit", method = RequestMethod.POST)
    public String edit(Model model, @ModelAttribute Edit${poNameC}Command command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            ${poNameC} ${poName} = new ${poNameC}();
                command.toPo(${poName});
            ${poName}.setUpdator(getCurrentUser().getId());
            ${poName}Service.update(${poName});
            model.addAttribute("no_error",true);
        }
        return "system/edit${poNameC}";
    }

    @RequestMapping(value = "/${poName}/add", method = RequestMethod.GET)
    public String showAdd(@ModelAttribute Edit${poNameC}Command command) {
        command.setStatus(1);
        command.setType(1);
        return "system/edit${poNameC}";
    }

    @RequestMapping(value = "/${poName}/add", method = RequestMethod.POST)
    public String add(Model model, @ModelAttribute Edit${poNameC}Command command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/edit${poNameC}";
        }
        ${poNameC} ${poName} = new ${poNameC}();
        command.toPo(${poName});
        ${poName}.setCreator(getCurrentUser().getId());
        ${poName}Service.add(${poName});
        attributes.addAttribute("id",${poName}.getId());
        attributes.addFlashAttribute("no_error",true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/${poName}/delete",method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id){
        logger.info("删除：" + id);
        ${poName}Service.delete(id);
        return new Response();
    }

    private void setAtt(Model model){

    }

}
